package com.exp.bookhouse.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exp.bookhouse.mapper.PermissionMapper;
import com.exp.bookhouse.pojo.Permission;
import com.exp.bookhouse.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 查询该用户的所有权限
     * @param userName
     * @return
     */
    @Override
    public List<Permission> queryPermissionsByUserName(String userName) {
        return permissionMapper.queryPermissionsByUserName(userName);
    }

    /**
     * 分页查询权限列表
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Permission> queryPermissionsByPage(Integer page, Integer size) {
        IPage<Permission> iPage = new Page<>(page,size);
        return permissionMapper.selectPage(iPage, null);
    }

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @Override
    public int savePermission(Permission permission) {
        return permissionMapper.insert(permission);
    }

    /**
     * 根据id查询权限信息
     * @param id
     * @return
     */
    @Override
    public Permission queryPermissionById(Long id) {
        return permissionMapper.selectById(id);
    }

    /**
     * 更新权限
     * @param permission
     * @return
     */
    @Override
    public int updatePermission(Permission permission) {
        return permissionMapper.updateById(permission);
    }

    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @Override
    public void deletePermission(Long id) {
        //删除权限时需将中间表对应的数据删除
        permissionMapper.deleteRolePermission(id);
        permissionMapper.deleteById(id);
    }
}
